﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace UCF.Core
{
    /// <summary>
    /// XmlNode扩展方法
    /// </summary>
    public static class ExMethodForXmlNode
    {
        /// <summary>
        /// 获取字符串值
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static string GetString(this XmlNode n)
        {
            return n == null ? "" : n.Value;
        }
        /// <summary>
        /// 获取数字值
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static int GetInteger(this XmlNode n)
        {
            return n == null ? 0 : n.Value.ToInt();
        }
        /// <summary>
        /// 获取时间值
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static DateTime GetDateTime(this XmlNode n)
        {
            return n == null ? DateTime.Parse("1900-1-1") : n.Value.ToDateTime();
        }

        /// <summary>
        /// XmlNodeList转换成Dictionary
        /// </summary>
        /// <param name="nl"></param>
        /// <returns></returns>
        public static Dictionary<string, string> ToDictionary(this XmlNodeList nl)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (XmlNode n in nl)
            {
                dict.Add(n.Name, n.InnerText);
            }
            return dict;
        }
        public static DataTable ToDataTable(this XmlNode n)
        {
            DataTable dt = new DataTable();
            foreach (XmlNode n1 in n.ChildNodes)
            {
                if (dt.Columns.Count == 0)
                {
                    foreach (XmlNode n2 in n1.ChildNodes)
                    {
                        dt.Columns.Add(n2.Name);
                    }
                }
                dt.Rows.Add(n1.ChildNodes.ToDictionary().Values.ToArray());
            }
            return dt;
        }

        #region XElement与XmlElement的转换

        /// <summary>
        /// XElement转换为XmlElement
        /// </summary>
        public static XmlElement ToXmlElement(XElement xElement)
        {
            if (xElement == null) return null;

            XmlElement xmlElement = null;
            XmlReader xmlReader = null;
            try
            {
                xmlReader = xElement.CreateReader();
                var doc = new XmlDocument();
                xmlElement = doc.ReadNode(xElement.CreateReader()) as XmlElement;
            }
            catch
            {
            }
            finally
            {
                if (xmlReader != null) xmlReader.Close();
            }

            return xmlElement;
        }

        /// <summary>
        /// XmlElement转换为XElement
        /// </summary>
        public static XElement ToXElement(XmlElement xmlElement)
        {
            if (xmlElement == null) return null;

            XElement xElement = null;
            try
            {
                var doc = new XmlDocument();
                doc.AppendChild(doc.ImportNode(xmlElement, true));
                xElement = XElement.Parse(doc.InnerXml);
            }
            catch { }

            return xElement;
        }

        public static Dictionary<string, string> ToDictionary(this XElement xe)
        {
            var q = from r in xe.Elements() select new { name = r.Name, id = r.Value };
            Dictionary<string,string> dict=new Dictionary<string,string>();
            q.All(a => { dict.Add(a.id, a.name.ToString()); return true; });
            return dict;
        }
        #endregion

    }
}
